package cn.lili.modules.supplier.serviceimpl;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.extend.ExtendEntityUtils;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.supplier.entity.dto.SupplierGoodsOperationDTO;
import cn.lili.modules.supplier.service.SupplierGoodsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应商商品 业务实现
 *
 * @author Chopper
 */
@Service
@RequiredArgsConstructor
public class SupplierGoodsServiceImpl implements SupplierGoodsService {

    private final GoodsClient goodsClient;


    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void addGoods(SupplierGoodsOperationDTO supplierGoodsOperationDTO) {
        //组织店铺信息
        AuthUser authUser = checkStoreAuthority();
        supplierGoodsOperationDTO.setStoreId(authUser.getExtendId());
        supplierGoodsOperationDTO.setStoreName(authUser.getExtendName());
        goodsClient.addSupplierGoods(supplierGoodsOperationDTO);
    }

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void editGoods(SupplierGoodsOperationDTO supplierGoodsOperationDTO) {

        //组织店铺信息
        AuthUser authUser = checkStoreAuthority();
        supplierGoodsOperationDTO.setStoreId(authUser.getExtendId());
        supplierGoodsOperationDTO.setStoreName(authUser.getExtendName());
        goodsClient.editSupplierGoods(supplierGoodsOperationDTO);
    }




    /**
     * 检查当前登录的店铺
     *
     * @return 当前登录的店铺
     */
    private AuthUser checkStoreAuthority() {
        AuthUser currentUser = UserContext.getCurrentUser();
        //如果当前会员不为空，且为店铺角色
        if (currentUser != null && ((currentUser.getScene().equals(SceneEnums.SUPPLIER) && currentUser.getExtendId() != null) || currentUser.getScene().equals(SceneEnums.MANAGER) && currentUser.getExtendId() != null)) {
            return currentUser;
        }
        throw new ServiceException(ResultCode.PERMISSIONS_DENIED);
    }

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void deleteGoods(List<String> goodsIds) {
        checkStoreAuthority();
        goodsClient.deleteGoodsV2(goodsIds);
    }

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public void editGoodsCategory(SupplierGoodsOperationDTO supplierGoodsOperationDTO) {
        checkStoreAuthority();
        goodsClient.editSupplierGoodsCategory(supplierGoodsOperationDTO);
    }

    @Override
    public ResultMessage<Object> addCategoryPathSon(String ids, String categoryPathSon, String categoryPathSonName) {
        goodsClient.addSupplierCategoryPathSon(ids, categoryPathSon, categoryPathSonName);
        return ResultUtil.data(true);
    }
}